#!/bin/sh

chmod +x ./*.sh
chmod +x ./scripts/*.sh

echo '--------------------------------------------------------------------------------------'
echo 'setup for' $1
echo '--------------------------------------------------------------------------------------'

. ./confs_$1.sh

#Create repository schema
sqlplus  $REPO_SCHEMA @$SCRIPTS_DIR/repo_schema.sql
sqlplus  $REPO_SCHEMA @$SCRIPTS_DIR/etl_my_ora.sql
sqlplus  $REPO_SCHEMA @$SCRIPTS_DIR/etl_my_ora_body.sql
sqlplus  $TARGET_SCHEMA @$SCRIPTS_DIR/is_mysql_find_enum.sql

# Metadata load and schema creation
$SCRIPTS_DIR/load_schema.sh

echo 'schema changer'
sqlplus  $REPO_SCHEMA @$ETL_HOME/confs\_$REF_DIR\_schema_changer.sql
echo 'mysql fill enum'
sqlplus  $TARGET_SCHEMA @$SCRIPTS_DIR/is_mysql_fill_enum.sql

# schema creator

sed 's;X_TMP_DIR;'${X_TMP_DIR}';g' $SCRIPTS_DIR/schema_creator.sql > $X_TMP_DIR/$REF_DIR\_schema_creator_tmp.sql
sed 's;mysql_schema;'${MYSQL_SCHEMA}';g' $X_TMP_DIR/$REF_DIR\_schema_creator_tmp.sql > $X_TMP_DIR/$REF_DIR\_schema_creator_tmp2.sql
sed 's;ref_dir;'${REF_DIR}';g' $X_TMP_DIR/$REF_DIR\_schema_creator_tmp2.sql > $X_TMP_DIR/$REF_DIR\_schema_creator_tmp3.sql
sed 's;my_super_user;'${MY_SUPER_USER}';g' $X_TMP_DIR/$REF_DIR\_schema_creator_tmp3.sql > $X_TMP_DIR/$REF_DIR\_schema_creator.sql

sed 's;X_TMP_DIR;'${X_TMP_DIR}';g' $SCRIPTS_DIR/conf_creator.sql > $X_TMP_DIR/$REF_DIR\_conf_creator_tmp2.sql
sed 's;mysql_schema;'${MYSQL_SCHEMA}';g' $X_TMP_DIR/$REF_DIR\_conf_creator_tmp2.sql > $X_TMP_DIR/$REF_DIR\_conf_creator_tmp3.sql
sed 's;mysql_type;'${MYSQL_TYPE}';g' $X_TMP_DIR/$REF_DIR\_conf_creator_tmp3.sql > $X_TMP_DIR/$REF_DIR\_conf_creator_tmp4.sql
sed 's;ref_dir;'${REF_DIR}';g' $X_TMP_DIR/$REF_DIR\_conf_creator_tmp4.sql > $X_TMP_DIR/$REF_DIR\_conf_creator.sql

sqlplus  $REPO_SCHEMA @$X_TMP_DIR/$REF_DIR\_schema_creator.sql
# schema creator
sqlplus $TARGET_SCHEMA  @$X_TMP_DIR/$REF_DIR\_schema_ddl.sql

# control file sql creator
sqlplus $REPO_SCHEMA  @$X_TMP_DIR/$REF_DIR\_conf_creator.sql
# Control file creation
sqlplus $REPO_SCHEMA  @$X_TMP_DIR/$REF_DIR\_spool_ctl.sql

sqlplus  $TARGET_SCHEMA @$ETL_HOME/confs\_$REF_DIR\_index_creator.sql

rm $X_TMP_DIR/*_tmp.sql
